<?php
namespace addons\ymystripe\services;

class Pay
{
    protected $config;


    public function __construct($config = false)
    {
        if(!$config){
            $config = get_addon_config('ymystripe');
        }
        // 密钥配置
        $this->config = $config;
    }

    public function checkout($pay_no,$price){
        $pay = model('app\api\model\wanlshop\Pay')
            ->where('pay_no', $pay_no)
            ->find();
        \Stripe\Stripe::setApiKey($this->config['secret']);
        $checkout_session = \Stripe\Checkout\Session::create([
            'line_items' => [[
                'amount' => $price * 100,
                'quantity' => 1,
                'currency' => 'usd',
                'name' => '商城支付',
            ]],
            'payment_method_types' => [

                'card',

            ],
            'mode' => 'payment',
            'success_url' => 'http://simonh5.akfapp.com/PaymentSuccessful?order_id='.$pay['order_id'],
            'cancel_url' => 'http://simonh5.akfapp.com/PaymentFailed?order_id='.$pay['order_id'],
        ]);
        $pay->allowField(true)
            ->save([
                'payment_intent' => $checkout_session->payment_intent
            ]);
        return $checkout_session;
    }
}